package io.jft.doll.room.repository

import io.jft.doll.api.UserApi
import io.jft.doll.api.apiProvider
import io.jft.doll.api.apiProviderMock
import io.jft.doll.room.entity.remote.InviteInfoREntity
import com.g.base.room.repository.BaseRepository
import com.g.base.room.repository.MockDbLiveData
import com.g.base.room.repository.NetworkBoundResult
import com.google.gson.JsonObject

/**
 * Created by G on 2017/11/13 0013.
 */
class UserRepository : BaseRepository() {
    companion object {
        val currentRemain by lazy { MockDbLiveData<String>().apply { postValue("0") } }
        val duibaAddress by lazy { MockDbLiveData<String>() }
        val inviteInfo by lazy { MockDbLiveData<InviteInfoREntity>() }
    }

    private val userApi by lazy { apiProvider.create(UserApi::class.java) }

    fun getRemainingSum(force: Boolean) =
            NetworkBoundResult(
                    {
                        userApi.remainingSum()
                    },
                    {
                        currentRemain
                    },
                    {
                        force || it.isNullOrEmpty()
                    },
                    {
                        currentRemain.postValue(it)
                    }
            )

    fun getSuggestCharge() =
            NetworkBoundResult(
                    {
                        apiProviderMock.create(UserApi::class.java).suggestCharge()
                    }
            )

    fun getBillsHistory(jsonObject: JsonObject) =
            NetworkBoundResult(
                    {
                        userApi.billsHistory(jsonObject)
                    }
            )

    fun makeCharge(jsonObject: JsonObject) =
            NetworkBoundResult(
                    {
                        userApi.makeCharge(jsonObject)
                    }
            )

    fun asyncChargeStatus(id: String) =
            NetworkBoundResult(
                    {
                        userApi.asyncCharge(id)
                    }
            )

    fun getDuiBarAddress() =
            NetworkBoundResult(
                    {
                        userApi.getDuiBarAddress()
                    },
                    {
                        duibaAddress
                    },
                    saveRemoteResult = { duibaAddress.postValue(it) }
            )

    fun getInviteInfo() =
            NetworkBoundResult(
                    {
                        userApi.getInviteInfo()
                    },
                    {
                        inviteInfo
                    },
                    saveRemoteResult = { inviteInfo.postValue(it) }
            )

    fun inputInviteCode( jsonObject: JsonObject) = NetworkBoundResult({userApi.inputInviteCode(jsonObject)})
}